package io.mft.web.adapter.api.builder;

import java.util.Map;

import io.mft.commons.function.RequestValue;
import io.mft.plugin.PluginResponse;
import io.mft.runtime.function.ApiFunctionResponse;
import io.mft.web.adapter.api.response.DataWebApiResponse;
import io.mft.web.adapter.api.response.WebApiResponse;

public class DataWebExecuteApiProtocolBuilder extends WebExecuteApiProtocolBuilder {

	@Override
	public RequestValue buildRequest(Map<String, Object> parameter) {
		RequestValue requestValue = new RequestValue();
		requestValue.putAll(parameter);
		// 分页参数
		if (parameter.get("__page") != null) {
			int currPage = Integer.parseInt((String) parameter.get("__page"));
			requestValue.set("pageNo", currPage);
		}
		if (parameter.get("__limit") != null) {
			int limit = Integer.parseInt((String) parameter.get("__limit"));
			requestValue.set("pageSize", limit);
		}
		// 对多级联动支持
		if (parameter.get("__seletPage") != null && parameter.get("__seletPage").equals("true")) {
			if (parameter.get("pageNumber") != null) {
				int currPage = Integer.parseInt((String) parameter.get("pageNumber"));
				requestValue.set("pageNo", currPage);
			}
			if (parameter.get("pageSize") != null) {
				int limit = Integer.parseInt((String) parameter.get("pageSize"));
				requestValue.set("pageSize", limit);
			}
		}
		return requestValue;
	}

	@Override
	public WebApiResponse buildWebApiResponse() {
		WebApiResponse response = new DataWebApiResponse();
		return response;
	}

	@Override
	public WebApiResponse buildWebApiResponse(ApiFunctionResponse response) {
		WebApiResponse webApiResponse = new DataWebApiResponse(response);
		return webApiResponse;
	}

}
